package com.ly.lw.styledo.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.ly.lw.styledo.R;

import java.util.ArrayList;

/**
 * 分类下部分碎片：推荐碎片+女装碎片
 *
 * @author 王学玲
 *         A simple {@link Fragment} subclass.
 */
public class CategoryFragmentHome extends Fragment implements RadioGroup.OnCheckedChangeListener {

    @ViewInject(R.id.category_radioGroup)
    private RadioGroup rgs;// 用于切换tab
    private ArrayList<Fragment> fragments;//一个tab页面对应一个Fragment

    private int fragmentContentId = R.id.category_container; // 将被替换的区域的id
    private int currentTab; // 当前Tab页面索引

    public CategoryFragmentHome() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_category_home, container, false);
        ViewUtils.inject(this, view);
        initView();
        return view;
    }

    private void initView() {
        fragments = new ArrayList<>();
        fragments.add(new CategoryFragment());
        fragments.add(new CategoryFragmentDis());
        fragments.add(CategoryManFragment.newInstance(10000050, 10000051));
        fragments.add(CategoryManFragment.newInstance(10000082, 10000083));
        fragments.add(CategoryManFragment.newInstance(10000101, 10000102));
        fragments.add(CategoryManFragment.newInstance(10000130, 10000131));
        fragments.add(CategoryManFragment.newInstance(10000204, 10000205));


        // 默认显示第一页
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.add(fragmentContentId, fragments.get(0));
        ft.commit();
        rgs.setOnCheckedChangeListener(this);
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        for (int i = 0; i < rgs.getChildCount(); i++) {
            if (rgs.getChildAt(i).getId() == checkedId) {
                Fragment fragment = fragments.get(i);
                FragmentTransaction ft = obtainFragmentTransaction(i);
                getCurrentFragment().onStop(); // 暂停当前tab
                if (fragment.isAdded()) {
                    fragment.onStart(); // 启动目标tab的fragment onStart()
//                    fragment.onResume(); // 启动目标tab的onResume()
                } else {
                    ft.add(fragmentContentId, fragment);
                    ft.commit();
                }
                showTab(i); // 显示目标tab
            }
        }
    }

    /**
     * 获取一个带动画的FragmentTransaction
     *
     * @param index
     * @return
     */
    private FragmentTransaction obtainFragmentTransaction(int index) {
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        // 设置切换动画
//        if (index > currentTab) {
//            ft.setCustomAnimations(R.anim.slide_left_in, R.anim.slide_left_out);
//        } else {
//            ft.setCustomAnimations(R.anim.slide_right_in, R.anim.slide_right_out);
//        }
        return ft;
    }

    public int getCurrentTab() {
        return currentTab;
    }

    public Fragment getCurrentFragment() {
        return fragments.get(currentTab);
    }

    /**
     * 切换tab
     *
     * @param idx
     */
    private void showTab(int idx) {
        for (int i = 0; i < fragments.size(); i++) {
            Fragment fragment = fragments.get(i);
            FragmentTransaction ft = obtainFragmentTransaction(idx);
            if (idx == i) {
                ft.show(fragment);
            } else {
                ft.hide(fragment);
            }
            ft.commit();
        }
        currentTab = idx; // 更新目标tab为当前tab
    }

}
